<!--
  -    Copyright (c) 2018-2025, infomng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: infomng
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-update="handleUpdate"
        @row-save="handleSave"
        @row-del="handleDel">
        <template slot="menuLeft">
          <el-button
            v-if="permissions.pfoc_devicedownparameter_export"
            class="filter-item"
            plain
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="exportExcel">导出
          </el-button>

          <el-button type="primary" icon="el-icon-plus" @click="addPatameter">添加</el-button>
        </template>
        <template slot-scope="scope" slot="menu">
          <el-button type="text"  icon="el-icon-edit"  size="small" plain  @click="editPatameter(scope.row)">编辑</el-button>
          <el-button type="text"  icon="el-icon-delete"  size="small" plain  @click="handleDel(scope.row,scope.index)">删除</el-button>
        </template>
      </avue-crud>
      <!--配置弹窗页面-->
      <el-dialog title=""
                 :visible.sync="configureDialogVisible"
                 width="60%"
                 lock-scroll
                 append-to-body
                 v-dialogDrag
                 :close-on-click-modal=false
                 v-if="configureDialogVisible"
                 :fullscreen="configureDialogFull">
        <template slot="title">
          <div class="avue-crud__dialog__header">
              <span class="el-dialog__title">
                  <span style="display:inline-block;background-color: #3478f5;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px"></span>
                  配置下行参数
              </span>
          </div>
        </template>

        <div class="pull-auto"  style="text-align: left;margin-left: 40px;font-size: 16px;color:#3478f5">
          <div class="block">
            设备名称：{{deviceName}}<i class="el-breadcrumb__separator el-icon-arrow-right"></i>
            设备编码：{{deviceSn}}
          </div>
          <div class="block">
          </div>
        </div>
        <!--配置页面-->
        <configurePage ref="downConfigurePageRef"  @closeConfigurePage="closeConfigurePage" parametersFlg="down" :id="id" :parameterNames="parameterName" :deviceParameterId="deviceParameterId" :deviceSn="deviceSn" :deviceName="deviceName" :dsIds="dsIds" :tableNames="tableNames" :columnNames="columnNames" :orderFields="orderFields"/>
      </el-dialog>
    </basic-container>
  </div>
</template>

<script>
import { fetchList, addObj, putObj, delObj } from '@/api/pfoc/devicedownparameter'
import { tableOption } from '@/const/crud/pfoc/devicedownparameter'
import { mapGetters } from 'vuex'
import configurePage from "@/views/pfoc/deviceparameterconfig/configure.vue";

export default {
  name: 'devicedownparameter',
  components: {configurePage},
  props:{
    deviceParameterId:{//设备参数配置ID
      type:String
    },
    deviceSn:{//设备编码
      type:String
    },
    deviceName:{ //设备名称
      type:String
    }
  },
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: tableOption,
      configureDialogVisible:false,//配置页面开关
      configureDialogFull:false,//配置页面弹窗缩放开关（默认放大）
      id:null,//下行参数ID（用于编辑）
      dsIds:null,//数据源ID
      tableNames:null,//表名称
      columnNames:null,//列字段
      orderFields:null,//排序字段
      parameterName:null,//参数名
    }
  },
  computed: {
    ...mapGetters(['permissions']),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.pfoc_devicedownparameter_add, false),
        delBtn: this.vaildData(this.permissions.pfoc_devicedownparameter_del, false),
        editBtn: this.vaildData(this.permissions.pfoc_devicedownparameter_edit, false)
      }
    }
  },
  methods: {
    // 列表查询
    getList(page, params) {
      this.tableLoading = true
      this.searchForm.deviceParameterId = this.deviceParameterId;
      fetchList(Object.assign({
        current: page.currentPage,
        size: page.pageSize
      }, params, this.searchForm)).then(response => {
        this.tableData = response.data.data.records
        this.page.total = response.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableLoading = false
      })
    },
    // 删除
    handleDel: function(row, index) {
      this.$confirm('是否确认删除ID为' + row.id, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delObj(row.id)
      }).then(data => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })
    },
    // 更新
    handleUpdate: function(row, index, done, loading) {
      putObj(row).then(data => {
        this.$message.success('修改成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 保存
    handleSave: function(row, done, loading) {
      addObj(row).then(data => {
        this.$message.success('添加成功')
        done()
        this.getList(this.page)
      }).catch(() => {
        loading()
      })
    },
    // 每页条数改变事件
    sizeChange(pageSize) {
      this.page.pageSize = pageSize
    },
    // 当前页发生改变事件
    currentChange(current) {
      this.page.currentPage = current
    },
    // 查询事件
    searchChange(form, done) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
      done()
    },
    // 刷新事件
    refreshChange() {
      this.getList(this.page)
    },
    //  导出excel
    exportExcel() {
      this.downBlobFile('/pfoc/devicedownparameter/export', this.searchForm, 'devicedownparameter.xlsx')
    },

    //打开新增配置页面
    addPatameter(){
      this.id = null;//下行参数ID（用于编辑）
      this.parameterName=null;//参数名
      this.dsIds=null;//数据源ID
      this.orderFields=null;//排序字段
      this.tableNames=null;//表名称
      this.columnNames=null;//列字段
      this.configureDialogVisible = true;//打开配置页面
    },
    //打开编辑配置页面
    editPatameter(row){
      this.id = row.id;//下行参数ID（用于编辑）
      this.deviceSn=row.deviceSn;//设备编码
      this.deviceName=row.deviceName;//设备名称
      this.parameterName=row.parameterName;//参数名
      this.deviceParameterId = row.deviceParameterId;//设备参数表ID
      this.dsIds=row.dataStore;//数据源ID
      this.orderFields=row.orderField;//排序字段
      this.tableNames=row.tableName;//表名称
      this.columnNames=row.columnName;//列字段
      //打开配置页面
      this.configureDialogVisible = true;
      this.$nextTick(() => {
        //调用配置页面回显方法
        this.$refs.downConfigurePageRef.configureInit();
      })

    },
    //关闭配置页面
    closeConfigurePage(){
      this.refreshChange();
      this.id = null;//下行参数ID（用于编辑）
      this.parameterName=null;//参数名
      this.dsIds=null;//数据源ID
      this.orderFields=null;//排序字段
      this.tableNames=null;//表名称
      this.columnNames=null;//列字段
      this.configureDialogVisible = false;//关闭配置页面
    }

  }
}
</script>
